<?php
/**
 * Class OAthLoginController
 * @package App\Http\Controllers\User
 * @author YWH
 * 第三方登陆
 */
namespace App\Http\Controllers\User;
use App\Http\Controllers\PCHController;
use App\Http\Controllers\System\SxbCenterController;
use App\Models\User\LiveUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\User\UserBaseModel;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\System\CoreController;
use App\Http\Models\Game\LiveModel;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\System\TSLSignController;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;


class LoginController extends Controller
{
    public $input;
    public $uploadToken;
    public function __construct()
    {
        parent::__construct();
//        $this->uploadToken = Controller::uploadToken ();
    }


    public function register(Request $request)
    {
        $input                      = $request->all();
        if (empty($input['user_name'])) {
            return self::response(null,'-1','用户名为空');
        }
        if (!preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$input['user_name'])) {
            return self::response(null,'-2','用户名由2-16位数字或字母、汉字、下划线组成！');
        }
        if (empty($input['user_password'])) {
            return self::response(null,'-3','密码为空');
        }
        if (mb_strlen($input['user_password'],'utf-8') < 1) {
            return self::response(null,'-4','密码必须为大于1位的字符串！');
        }
        if (empty($input['invite_code'])) {
            return self::response(null,'-5','邀请码为空');
        }
        if (mb_strlen($input['invite_code'],'utf-8') < 4) {
            return  self::response(null,-6,'邀请码不能少于4位哦');
        }
        if (mb_strlen($input['invite_code'],'utf-8') > 20) {
            return  self::response(null,-7,'邀请码不能大于20位哦');
        }
        $user = LiveUser::getUserByUserName($input['user_name']);
        if ($user) {
            return  self::response(null,-8,'用户名已被使用');
        }
        $invite_user_id = LiveUser::getUserIDByInviteCode($input['invite_code']);
        if (!$invite_user_id) {
            return  self::response(null,-9,'邀请码不存在');
        }
        unset($input['invite_code']);
        $input['invite_user_id'] = $invite_user_id;
        $input['created_time'] = time();
        $user_id = LiveUser::createUser($input);
        if ($user_id) {
            $token  = LiveUser::UpdateToken($user_id,$request->ip());
            $user   = LiveUser::getUserByUserId($user_id);
            if (!empty($user)) {
                $user->user_token = $token;
                $user = $user->toArray();
                unset($user['user_password']);
            }
            return self::response($user,200);
        } else {
            return self::response(null,-10,'操作失败，稍后再试');
        }
    }

    public function sign(Request $request)
    {
        $input                      = $request->all();
        if (empty($input['user_name'])) {
            return self::response(null,'-1','用户名为空');
        }
        if (!preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$input['user_name'])) {
            return self::response(null,'-2','用户名由2-16位数字或字母、汉字、下划线组成！');
        }
        if (empty($input['user_password'])) {
            return self::response(null,'-3','密码为空');
        }
        if (mb_strlen($input['user_password'],'utf-8') < 1) {
            return self::response(null,'-4','密码必须为大于1位的字符串！');
        }
        $user = LiveUser::getUserByUserName($input['user_name']);
        if (!$user) {
            return self::response(null,'-5','用户不存在');
        }
        if (!password_verify($input['user_password'],$user->user_password)) {
            return self::response(null,'-6','密码错误');
        }
        if ($user->status == 0) {
            return self::response(null,'-7','您的账号已被禁用哦~');
        }
        $token = LiveUser::UpdateToken($user->id,$request->ip());
        if (!$token) {
            return self::response(null,'-8','更新token失败');
        }
        unset($user->user_password);
        $user->user_token = $token;
        return self::response($user,'200');
    }

    public function reset(Request $request)
    {
        $input                      = $request->all();
        if (empty($input['user_id'])) {
            return self::response(null,'-1','用户ID为空');
        }
        if (empty($input['old_password'])) {
            return self::response(null,'-2','原密码为空');
        }
        if (empty($input['new_password'])) {
            return self::response(null,'-3','新密码为空');
        }
        if (mb_strlen($input['new_password'],'utf-8') < 1) {
            return self::response(null,'-4','新密码必须为大于1位的字符串！');
        }
        $user = LiveUser::getUserByUserId($input['user_id']);
        if (empty($user)) {
            return self::response(null,'-5','用户不存在');
        }
        if (!password_verify($input['old_password'],$user->user_password)) {
            return self::response(null,'-6','原密码错误');
        }
        $flag = LiveUser::modifyPassword($input['user_id'],$input['new_password']);
        if (empty($flag)) {
            return self::response(null,'-7','系统繁忙');
        }
        return self::response(null,'200');
    }










}
